package com.wzx.user.utils.quartzUtils;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/5/17 - 17:09
 * @description 标题
 */

import com.wzx.common.exception.AutoApiResponse;
import com.wzx.user.service.RedPkgService;
import com.wzx.user.utils.SpringContextJobUtil;
import com.wzx.user.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 定时任务测试类
 */
@Slf4j
@Component
public class TestJob1 implements Job {

    /*@Autowired
    private RedPkgService redPkgService;*/

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //RedPkgService redPkgService = (RedPkgService) SpringUtil.getBean("redPkgServiceImpl");
        RedPkgService redPkgService = (RedPkgService) SpringContextJobUtil.getBean("redPkgServiceImpl");
        redPkgService.backPrice();
        log.info("定时任务开始：{}", new Date());
    }

    /**
     * @Description: 定时任务job1
     * @Param [scheduler]
     * @Return: void
     **/
    public void startJob1(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(TestJob1.class)
                .withIdentity("job1", "group1")
                .build();
        // 基于表达式构建触发器 //5分钟执行一次 ("0 0/1 * * * ?")    0/10 * * * * ?
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job1", "group1")
                .withDescription("测试任务描述job1")
                .withSchedule(cronScheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}
